package com.ywy.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * RabbitMQ配置
 *
 * @author ywy
 * @date 2021/12/30 15:58
 */
@Configuration
public class RabbitMqConfig {
    /**
     * 队列名称
     */
    public static final String ES_QUEUE = "es_queue";

    /**
     * 交换机名称
     */
    public static final String ES_EXCHANGE = "es_exchange";

    /**
     * 路由键名称
     */
    public static final String ES_BIND_KEY = "es_bind_key";

    /**
     * 声明队列
     * @return
     */
    @Bean
    public Queue queue() {
        return new Queue(ES_QUEUE);
    }

    /**
     * 声明交换机
     * Direct路由模式
     * @return
     */
    @Bean
    public DirectExchange exchange() {
        return new DirectExchange(ES_EXCHANGE);
    }

    /**
     * 绑定交换机和队列
     * @param queue
     * @param exchange
     * @return
     */
    @Bean
    public Binding bindingExchangeMessage(Queue queue, DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(ES_BIND_KEY);
    }
}
